import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/controllers/seckill_controller/seckill_page_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/vo/seckill_list_vo.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class SeckillGoodsArr extends StatelessWidget {

  final List<SeckillGoodItem> dataArr;
  final SeckillPageController controller;
  SeckillGoodsArr({
    required this.dataArr,
    required this.controller,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      margin: EdgeInsets.only(top: 12.h),
      child: StaggeredGridView.countBuilder(
        padding: EdgeInsets.zero,
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        crossAxisCount: 4,
        itemCount: dataArr.length,
        itemBuilder: (BuildContext context, int index) {
          SeckillGoodItem item = dataArr[index];
          return Container(
            width: 165.w,
            decoration: BoxDecoration(
              color: bgWhiteColor,
              borderRadius: BorderRadius.circular(5.w),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Stack(
                  children: [
                    NetworkImageUtil(
                      url: item.image ?? '', 
                      width: 165.w, 
                      height: 165.w,
                      topLeftRadius: 5.w,
                      topRightRadius: 5.w,
                    ),
                    Positioned(
                      top: 0,
                      left: 0,
                      child: ClipRRect(
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(5.w)
                        ),
                        child: Image.asset(
                          'assets/icons/icon_flash_sale_bar.png',
                          width: 72.w,
                          height: 19.h,
                          fit: BoxFit.cover,
                        )
                      ),
                    )
                  ],
                ),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 6.w, vertical: 12.h),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '${item.title}',
                        overflow: TextOverflow.ellipsis,
                        maxLines: 2,
                        style: TextStyle(
                          color: textBlackColor,
                          fontSize: 14.sp,
                          fontWeight: FontWeight.bold
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.only(top: 6.h, bottom: 12.h, left: 6.w, right: 6.w),
                        child: Text.rich(
                          TextSpan(
                            text: '抢购价：',
                            style: TextStyle(
                              color: mainTextColor,
                              fontSize: 14.sp
                            ),
                            children: [
                              TextSpan(
                                text: '¥',
                                style: TextStyle(
                                  color: mainTextColor,
                                  fontSize: 12.sp,
                                  fontWeight: FontWeight.bold
                                ),
                              ),
                              TextSpan(
                                text: '${item.price}',
                                style: TextStyle(
                                  color: mainTextColor,
                                  fontSize: 16.sp,
                                  fontWeight: FontWeight.bold
                                ),
                              ),
                            ]
                          )
                        ),
                      ),
                      ButtonUtil(
                        text: '立即抢购', 
                        fontSize: 14.sp, 
                        borderRadius: 20.w, 
                        height: 30.h, 
                        onTop: () {
                          Get.toNamed(
                            '${AppRouters.COMMON_PAGE}${AppRouters.SECKILL_PAGE}${AppRouters.SECKILL_DETAIL_PAGE}',
                            arguments: {
                              'id': item.id
                            }
                          )!.then((res) {
                            controller.getSeckillList(showLoading: false);
                          });
                        }
                      )
                    ],
                  ),
                )
              ],
            ),
          );
        },
        staggeredTileBuilder: (int index) => StaggeredTile.fit(2),
        mainAxisSpacing: 12.h,
        crossAxisSpacing: 13.w,
      ),
    );
  }
}